package main

type ListNode struct {
	Val  int
	Next *ListNode
}

func removeElements(head *ListNode, val int) *ListNode {
	hair := &ListNode{Next: head}
	n := head
	pre := hair
	for n != nil {
		if n.Val == val {
			pre.Next = n.Next
			n = n.Next
		} else {
			pre = n
			n = n.Next
		}
	}
	return hair.Next
}

func main() {
	n0 := &ListNode{Val: 1}
	n1 := &ListNode{Val: 2}
	n2 := &ListNode{Val: 3}
	n3 := &ListNode{Val: 4}
	n4 := &ListNode{Val: 5}
	n0.Next = n1
	n1.Next = n2
	n2.Next = n3
	n3.Next = n4
	removeElements(n0, 1)
}
